216B - Forming Teams - CodeForces Solution


dfs and similar implementation *1700

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#define ll  long long
#define _for(i, n)      for(ll i = 0; i < n; i++)
#define _forv(i, a, n)      for(ll i = a; i < n; i++)
#define _forvRev(i, a, n)      for(ll i = a; i >= n; i--)
#define _for1(i, n)      for(ll i = 1; i <= n; i++)
#define _fore(k,v,mp)    for (const auto& [k, v] : mp)
#define vbe(vec)    vec.begin(), vec.end()
#define uniqueVec(vec) vec.resize(distance(vec.begin(), unique(vbe(vec))));
#define mp(f,s)     make_pair(f, s)
#define yes     cout<<"Yes"<<endl
#define no      cout<<"No"<<endl
using namespace std;
void decToBinary(ll n,string &s)
{
    int binaryNum[100];
    int i = 0;
    if(n==0)s+="0";
    while (n > 0)
    {
        binaryNum[i] = n % 2;

        n = n / 2;

        i++;

    }
    for (int j = i - 1; j >= 0; j--)

        s+=to_string(binaryNum[j]);
}
inline void f()
{
    ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
}
ll factorial(ll num)
{
    if (num == 0)
        return 1;
    else
        return(num * factorial(num - 1));
}
ll gcd (ll a,ll b)
{
    if(a==0)return b;
    return gcd(b%a,a);
}
ll lcm (ll a,ll b)
{
    return (a*b)/ gcd(a,b);
}
ll binarySearch(ll arr[], ll l, ll r, ll x)
{
    if (r >= l) {
        ll mid = l + (r - l) / 2;

        if (arr[mid] == x)
            return mid;

        if (arr[mid] > x)
            return binarySearch(arr, l, mid - 1, x);

        return binarySearch(arr, mid + 1, r, x);
    }

    return -1;
}

vector<ll> G[101];
ll benched=0;
bool fl=true;
map<ll,ll>mp;
ll visited[1001]={0};
void DFS(ll x){
visited[x]=1;
//cout<<x<<"-";
benched++;
_for(i,G[x].size()){
if(!visited[G[x][i]]&&fl)DFS(G[x][i]);
if(G[x].size()==1){fl=false;break;}

}

}
const ll N=1000000;
const double PI =3.14159265;
const ll MOD=1e9+7;

map <ll,ll> mp1;
map<ll,bool> BCD;
map <char,string> mp2;
map <ll,ll> mp3;
vector <ll>v;
vector <ll>vec2;
vector <ll>vec1[101];
queue<char> q;
set<ll> s;
//pair<ll,ll>hourcnt,hourint;
int main()
{
    //freopen(".in", "r", stdin);
    f();
     ll n,m,bench=0;//bool fl= false;
      cin>>n>>m;
     _for(i,m){
         ll a,b;
         cin>>a>>b;
         G[a].push_back(b);
         G[b].push_back(a);
     }
     _for1(i,n){
     DFS(i);
     if(benched%2!=0&&benched>1&&fl){bench++;}
     //cout<<endl;
     //cout<<"("<<benched<<")"<<endl;
     benched=0;
     fl=true;
     }
    if((n-bench)%2==0)cout<<bench<<endl;
    else cout<<bench+1<<endl;
    return 0;
}
/*

*/


Comments

Submit
0 Comments
More Questions

3. Longest Substring Without Repeating Characters
1312. Minimum Insertion Steps to Make a String Palindrome
1092. Shortest Common Supersequence
1044. Longest Duplicate Substring
1032. Stream of Characters
987. Vertical Order Traversal of a Binary Tree
952. Largest Component Size by Common Factor
212. Word Search II
174. Dungeon Game
127. Word Ladder
123. Best Time to Buy and Sell Stock III
85. Maximal Rectangle
84. Largest Rectangle in Histogram
60. Permutation Sequence
42. Trapping Rain Water
32. Longest Valid Parentheses
Cutting a material
Bubble Sort
Number of triangles
AND path in a binary tree
Factorial equations
Removal of vertices
Happy segments
Cyclic shifts
Zoos
Build a graph
Almost correct bracket sequence
Count of integers
Differences of the permutations
Doctor's Secret